import { Geometry } from './geometry'

/**
 * @ClassName polygon
 * @Description 面
 * @Author xuyizhuo
 * @Date 2024/1/20 14:06
 */
export class Polygon extends Geometry {
    // 面内有环
    // 如：
    // coordinates: number[][][] = [[[1, 1], [2, 2], [1, 2]], [[1.5, 1.5], [1.9, 1.9], [1.5, 1.9]]]
    // 面的点坐标数组
    private _coordinates: number[][][]

    constructor(coordinates: number[][][]) {
        super()
        this._coordinates = coordinates
    }

    // 绘制图形
    draw(ctx: CanvasRenderingContext2D) {
        ctx.save()
        ctx.strokeStyle = '#ff0000'
        ctx.fillStyle = '#ff0000'
        ctx.lineWidth = 2
        ctx.beginPath()

        for (let i = 0; i < this._coordinates.length; i++) {
            const ring = this._coordinates[i]
            for (let j = 0; j < ring.length; j++) {
                const [x, y] = ring[j]
                if (j === 0) {
                    ctx.moveTo(x, y)
                } else {
                    ctx.lineTo(x, y)
                }
            }
        }

        // 闭合路径
        ctx.closePath()
        ctx.fill('evenodd')
        ctx.stroke()
        ctx.restore()
    }
}
